package com.beiing.tianshuai.tianshuai.message.ui;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.MyFriendsAdapter;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.ContactsBean;
import com.beiing.tianshuai.tianshuai.entity.MyFriendListBean;
import com.beiing.tianshuai.tianshuai.mine.ui.PersonalDataCardActivity;
import com.beiing.tianshuai.tianshuai.presenter.MyFriendListPresenter;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.util.PinYinUtils;
import com.beiing.tianshuai.tianshuai.view.MyFriendListViewImpl;
import com.beiing.tianshuai.tianshuai.widget.CustomProgressDialog;
import com.beiing.tianshuai.tianshuai.widget.SideLitterBarView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Jinni on 2017/7/24.
 */

public class MyFriendsActivity extends BaseActivity implements MyFriendListViewImpl {

    private Handler                             mHandler;
    private List<ContactsBean>                  mContactList;
    private String                              mUid;

    @BindView(R.id.toolbar_iv_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_tv_title)
    TextView mToolbarTitle;
    @BindView(R.id.my_friends_list)
    ListView listView;
    @BindView(R.id.side_navigation)
    SideLitterBarView wordsNavigation;
    @BindView(R.id.tv_center)
    TextView tvCenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_myfriends;
    }

    @Override
    protected void init() {
        Intent intent = getIntent();
        mUid = intent.getStringExtra("uid");
        mHandler = new Handler();
        initToolbar();
        initListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initPresenter();
    }

    private void initPresenter() {
        MyFriendListPresenter presenter = new MyFriendListPresenter(this);
        presenter.getMyFriendList(mUid);
    }

    private void initToolbar() {
        mToolbarBack.setVisibility(View.VISIBLE);
        mToolbarBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mToolbarTitle.setText("通讯录");
    }

    private void initListener() {
        // 点击好友
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (mContactList != null) {
                    ContactsBean friend = mContactList.get(i);
                    String friendUid = friend.getUid();
                    Intent intent = new Intent(mContext, PersonalDataCardActivity.class);
                    intent.putExtra("uid", friendUid);
                    intent.putExtra("isFriend", true);
                    startActivity(intent);
                }
            }
        });

        // 快捷导航栏
        wordsNavigation.setListener(new SideLitterBarView.onWordsChangeListener() {
            @Override
            public void wordsChange(String words) {
                if (mContactList != null) {
                    updateWord(words);
                    updateListView(words);
                }
            }
        });

        // listView设置滑动监听
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
                if (mContactList != null) {
                    wordsNavigation.setTouchIndex(mContactList.get(i).getFirstLetter());
                }
            }
        });
    }

    private void updateWord(String words) {
        tvCenter.setText(words);
        tvCenter.setVisibility(View.VISIBLE);
        mHandler.removeCallbacksAndMessages(null);
        //500ms后消失显示
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                tvCenter.setVisibility(View.GONE);
            }
        }, 500);
    }

    private void updateListView(String words) {
        for (int i = 0; i < mContactList.size(); i++) {
            //将手指按下的字母与列表中相同字母开头的项找出
            if (words.equals(mContactList.get(i).getFirstLetter())) {
                listView.setSelection(i);
                break;
            }
        }
    }

    @Override
    public void showProgress() {
        CustomProgressDialog.showLoading(mContext, "正在努力加载，请稍后...", true);
    }

    @Override
    public void hideProgress() {
        CustomProgressDialog.stopLoading();
    }

    @Override
    public void onRequestSuccess(MyFriendListBean bean) {
        List<MyFriendListBean.DataBean.NumberBean> friends = bean.getData().getNumber();
        if (friends.size() > 0) {
            mContactList = new ArrayList<>();
            MyFriendsAdapter adapter = new MyFriendsAdapter(this, mContactList);
            listView.setAdapter(adapter);
            for (MyFriendListBean.DataBean.NumberBean friend : friends) {
                ContactsBean contactBean = new ContactsBean();
                try {
                    String name = friend.getName();
                    String uid = friend.getUid();
                    String avatar = friend.getPhoto();
                    String unit = friend.getUnit();
                    contactBean.setName(name);
                    contactBean.setAvatar(avatar);
                    contactBean.setUid(uid);
                    contactBean.setUnit(unit);
                    contactBean.setFirstLetter(PinYinUtils.getPinyin(name).substring(0, 1));
                    mContactList.add(contactBean);
                } catch (Exception e) {
                    LogUtils.i(friend.toString(), "FriendList");
                    e.printStackTrace();
                }
            }
            Collections.sort(mContactList, new Comparator<ContactsBean>() {
                @Override
                public int compare(ContactsBean contactBean, ContactsBean t1) {
                    return contactBean.getFirstLetter().compareTo(t1.getFirstLetter());
                }
            });
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        LogUtils.e(throwable.getMessage(), "MyFriendsActivity");
        showShortText("网络异常");
    }
}
